<?php

namespace Admin\Models\school;

use Admin\Models\BaseModel;
class XhschoolAppstore extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iSASId;

    /**
     *
     * @var integer
     */
    public $iAppId;

    /**
     *
     * @var integer
     */
    public $bSASEnable;

    /**
     *
     * @var integer
     */
    public $bSASDown;

    /**
     *
     * @var integer
     */
    public $iSchoolId;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("XHSchool_AppStore");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSchool_AppStore';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolAppstore[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolAppstore
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据ID删除对应记录
     *
     * @param $idArr
     * @return XhschoolAppstore[]
     *
     * create by ck 20160927
     */
    public static function deleteRecordById($idArr)
    {
        return self::find(array(
            'conditions'    => 'iSASId IN ({idArr:array}) AND bDelete = 0',
            'bind'          => array('idArr' => $idArr)
        ))->delete();
    }

    /**
     * 创建新model
     *
     * @param $schoolId
     * @param $appId
     * @return XhschoolAppstore
     *
     * create by ck 20160928
     */
    public static function createRecord($schoolId, $appId)
    {
        $obj = new self;
        $obj->iAppId        = $appId;
        $obj->iSchoolId     = $schoolId;
        $obj->bSASDown      = 1;
        $obj->bSASEnable    = 1;
        return $obj;
    }

    /**
     * 学校已关联的应用信息
     *
     * @param $schoolId
     * @return mixed
     *
     * create by ck 20160927
     */
    public static function createBuilderForInAppInfoBySchoolId($schoolId)
    {
        return \Phalcon\Di::getDefault()->get('modelsManager')->createBuilder()
            ->columns(['B.iAppCode', 'B.sAppName', 'B.sAppPackage', 'B.iAppType', 'B.sAppVersion', 'B.iAppSystem', 'B.bAppHistory',
                'A.iSASId', 'A.bSASEnable', 'A.bSASDown'])
            ->from(['A' => '\\Admin\\Models\\school\\XhschoolAppstore'])
            ->leftJoin('\\Admin\\Models\\app\\XhappStore', 'A.iAppId = B.iAppId', 'B')
            ->where('A.iSchoolId = :iSchoolId: AND A.bDelete = 0 AND B.bDelete = 0', ['iSchoolId' => $schoolId])
            ->orderBy('B.iAppCode');
    }

    /**
     * 学校不包含的应用信息
     *
     * @param $schoolId
     * @return mixed
     *
     * create by ck 20160927
     */
    public static function createPhqlForOutAppInfoBySchoolId($schoolId)
    {
        $queryArr['columns'] = ['iAppCode', 'sAppName', 'sAppPackage', 'iAppType', 'sAppVersion', 'iAppSystem'];
        $queryArr['query']   = "SELECT :columns: FROM \\Admin\\Models\\app\\XhappStore AS A WHERE A.bDelete=0 and A.bAppHistory = 0 AND A.sAppPackage NOT IN (select C.sAppPackage FROM \\Admin\\Models\\school\\XhschoolAppstore AS B LEFT JOIN \\Admin\\Models\\app\\XhappStore AS C ON B.iAppId = C.iAppId WHERE B.bDelete=0 AND C.bDelete = 0 AND B.iSchoolId = {$schoolId}) ORDER BY A.iAppCode";
        return $queryArr;
    }

    /**
     * 更新学校应用
     *
     * @param $schoolId
     * @param $SASIds      
     * @return mixed
     *
     * create by ck 20160928
     */
    public static function updateSchoolAppBySchoolId($schoolId, $SASIds)
    {
        $updateSchoolAppStr = "UPDATE XHSchool_AppStore AS A LEFT JOIN XHApp_Store AS B ON A.iAppId = B.iAppId LEFT JOIN XHApp_Store AS C ON B.sAppPackage = C.sAppPackage SET A.iAppId = C.iAppId WHERE A.bDelete = 0 AND C.bDelete = 0 AND C.bAppHistory = 0 AND  A.iSchoolId = {$schoolId}";
        if($SASIds != 'all') $updateSchoolAppStr .= " AND A.iSASId IN ({$SASIds})";
        return \Phalcon\Di::getDefault()->get('db')->query($updateSchoolAppStr)->execute();
    }

}
